﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Com.Rivyer.Web.contact
{
    public partial class commonnum : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //非法操作验证
            if (Session["username"] == null
                || Session["username"].ToString().Trim().Length == 0)
            {
                Response.Write("<script>top.location='./login.aspx'</script>");
            }
            else
            {
                if (new BLL.userauthorityBll().GetModel(Session["username"].ToString()).isadmin == 1)
                {
                    commoncontact.Columns[0].Visible = true;
                }
                else
                {
                    commoncontact.Columns[0].Visible = false;
                }
            }
        }

        protected void commoncontact_RowUpdated(object sender, DevExpress.Web.Data.ASPxDataUpdatedEventArgs e)
        {
            Model.useroperatelogModel logmodel = new Model.useroperatelogModel();
            logmodel.createdate = DateTime.Now;
            logmodel.operation = "更新";
            logmodel.username = Session["username"].ToString();
            logmodel.description = string.Format("常用联系人;记录数:{0}", e.AffectedRecords);
            new BLL.useroperatelogBll().Add(logmodel);
        }

        protected void commoncontact_RowInserted(object sender, DevExpress.Web.Data.ASPxDataInsertedEventArgs e)
        {
            Model.useroperatelogModel logmodel = new Model.useroperatelogModel();
            logmodel.createdate = DateTime.Now;
            logmodel.operation = "新增";
            logmodel.username = Session["username"].ToString();
            logmodel.description = string.Format("常用联系人;记录数:{0}", e.AffectedRecords);
            new BLL.useroperatelogBll().Add(logmodel);
        }

        protected void commoncontact_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            if (e.NewValues["department"] == null
                && e.NewValues["contactname"] == null)
            {
                e.Cancel = true;
            }
            if (e.NewValues["telephonenum"] == null
                && e.NewValues["mobilephone"] == null)
            {
                e.Cancel = true;
            }

            if (e.NewValues["contactname"] != null
                && !Maticsoft.Common.PageValidate.IsCHZN(e.NewValues["contactname"].ToString()))
            {
                e.Cancel = true;
            }

            if (e.NewValues["telephonenum"] != null
                && !Maticsoft.Common.PageValidate.IsNumber(e.NewValues["telephonenum"].ToString()))
            {
                e.Cancel = true;
            }

            if (e.NewValues["mobilephone"] != null
               && !Maticsoft.Common.PageValidate.IsMobilePhone(e.NewValues["mobilephone"].ToString()))
            {
                e.Cancel = true;
            }


        }

        protected void commoncontact_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            if (e.NewValues["department"] == null
                && e.NewValues["contactname"] == null)
            {
                e.Cancel = true;
            }
            if (e.NewValues["telephonenum"] == null
                && e.NewValues["mobilephone"] == null)
            {
                e.Cancel = true;
            }

            if (e.NewValues["contactname"] != null
                && !Maticsoft.Common.PageValidate.IsCHZN(e.NewValues["contactname"].ToString()))
            {
                e.Cancel = true;
            }

            if (e.NewValues["telephonenum"] != null
                && !Maticsoft.Common.PageValidate.IsNumber(e.NewValues["telephonenum"].ToString()))
            {
                e.Cancel = true;
            }

            if (e.NewValues["mobilephone"] != null
               && !Maticsoft.Common.PageValidate.IsMobilePhone(e.NewValues["mobilephone"].ToString()))
            {
                e.Cancel = true;
            }
        }

        protected void btn_export_Click(object sender, EventArgs e)
        {
            ASPxGridViewExporter1.WriteXlsToResponse(string.Format("常用联系人"));
            new BLL.useroperatelogBll().AddLog(Session["username"].ToString(), "下载", "常用联系人");
        }
    }
}